package com.rxjava2.android.samples.ui.operators;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rxjava2.android.samples.R;
import com.rxjava2.android.samples.utils.AppConstant;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.ReplaySubject;

/**
 * Created by amitshekhar on 17/12/16.
 */

public class ReplaySubjectExampleActivity extends AppCompatActivity {

  private static final String TAG = ReplaySubjectExampleActivity.class.getSimpleName();
  Button btn;
  TextView textView;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example);
    btn = (Button) findViewById(R.id.btn);
    textView = (TextView) findViewById(R.id.textView);

    btn.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        doSomeWork();
      }
    });
  }

  /* ReplaySubject emits to any observer all of the items that were emitted
   * by the source Observable, regardless of when the observer subscribes.
   */
  private void doSomeWork() {

    ReplaySubject<Integer> source = ReplaySubject.create();

    source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4
    source.subscribe(getSecondObserver());// 1, 2, 3, 4

    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    source.onNext(4);
    source.onComplete();

        /*
         * it will emit 1, 2, 3, 4 for third observer also as we have used replay
         */
    source.subscribe(getThirdObserver());

  }

  private Observer<Integer> getFirstObserver() {
    return new Observer<Integer>() {

      @Override public void onSubscribe(Disposable d) {
        Log.d(TAG, " First onSubscribe : " + d.isDisposed());
      }

      @Override public void onNext(Integer value) {
        textView.append(" First onNext : value : " + value);
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " First onNext value : " + value);
      }

      @Override public void onError(Throwable e) {
        textView.append(" First onError : " + e.getMessage());
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " First onError : " + e.getMessage());
      }

      @Override public void onComplete() {
        textView.append(" First onComplete");
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " First onComplete");
      }
    };
  }

  private Observer<Integer> getSecondObserver() {
    return new Observer<Integer>() {

      @Override public void onSubscribe(Disposable d) {
        textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed());
        Log.d(TAG, " Second onSubscribe : " + d.isDisposed());
        textView.append(AppConstant.LINE_SEPARATOR);
      }

      @Override public void onNext(Integer value) {
        textView.append(" Second onNext : value : " + value);
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " Second onNext value : " + value);
      }

      @Override public void onError(Throwable e) {
        textView.append(" Second onError : " + e.getMessage());
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " Second onError : " + e.getMessage());
      }

      @Override public void onComplete() {
        textView.append(" Second onComplete");
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " Second onComplete");
      }
    };
  }

  private Observer<Integer> getThirdObserver() {
    return new Observer<Integer>() {

      @Override public void onSubscribe(Disposable d) {
        textView.append(" Third onSubscribe : isDisposed :" + d.isDisposed());
        Log.d(TAG, " Third onSubscribe : " + d.isDisposed());
        textView.append(AppConstant.LINE_SEPARATOR);
      }

      @Override public void onNext(Integer value) {
        textView.append(" Third onNext : value : " + value);
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " Third onNext value : " + value);
      }

      @Override public void onError(Throwable e) {
        textView.append(" Third onError : " + e.getMessage());
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " Third onError : " + e.getMessage());
      }

      @Override public void onComplete() {
        textView.append(" Third onComplete");
        textView.append(AppConstant.LINE_SEPARATOR);
        Log.d(TAG, " Third onComplete");
      }
    };
  }
}